{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Positions of Large Groups"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #string"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: largeGroupPositions"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #较大分组的位置"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>在一个由小写字母构成的字符串 <code>s</code> 中，包含由一些连续的相同字符所构成的分组。</p>\n",
    "\n",
    "<p>例如，在字符串 <code>s = \"abbxxxxzyy\"</code> 中，就含有 <code>\"a\"</code>, <code>\"bb\"</code>, <code>\"xxxx\"</code>, <code>\"z\"</code> 和 <code>\"yy\"</code> 这样的一些分组。</p>\n",
    "\n",
    "<p>分组可以用区间 <code>[start, end]</code> 表示，其中 <code>start</code> 和 <code>end</code> 分别表示该分组的起始和终止位置的下标。上例中的 <code>\"xxxx\"</code> 分组用区间表示为 <code>[3,6]</code> 。</p>\n",
    "\n",
    "<p>我们称所有包含大于或等于三个连续字符的分组为 <strong>较大分组</strong> 。</p>\n",
    "\n",
    "<p>找到每一个 <strong>较大分组</strong> 的区间，<strong>按起始位置下标递增顺序排序后</strong>，返回结果。</p>\n",
    "\n",
    "<p> </p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>s = \"abbxxxxzzy\"\n",
    "<strong>输出：</strong>[[3,6]]\n",
    "<strong>解释</strong><strong>：</strong><code>\"xxxx\" 是一个起始于 3 且终止于 6 的较大分组</code>。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>s = \"abc\"\n",
    "<strong>输出：</strong>[]\n",
    "<strong>解释：</strong>\"a\",\"b\" 和 \"c\" 均不是符合要求的较大分组。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 3：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>s = \"abcdddeeeeaabbbcd\"\n",
    "<strong>输出：</strong>[[3,5],[6,9],[12,14]]\n",
    "<strong>解释：</strong>较大分组为 \"ddd\", \"eeee\" 和 \"bbb\"</pre>\n",
    "\n",
    "<p><strong>示例 4：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>s = \"aba\"\n",
    "<strong>输出：</strong>[]\n",
    "</pre>\n",
    " \n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 <= s.length <= 1000</code></li>\n",
    "\t<li><code>s</code> 仅含小写英文字母</li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [positions-of-large-groups](https://leetcode.cn/problems/positions-of-large-groups/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [positions-of-large-groups](https://leetcode.cn/problems/positions-of-large-groups/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['\"abbxxxxzzy\"', '\"abc\"', '\"abcdddeeeeaabbbcd\"']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largeGroupPositions(self, s: str) -> List[List[int]]:\n",
    "        # 对一个元素，固定第一个指针，从该指针起遍历，直到第二个指针不等于它\n",
    "        l = 0\n",
    "        r = 1\n",
    "        out = []\n",
    "        while r < len(s):\n",
    "            if s[l] == s[r]:\n",
    "                r += 1\n",
    "            else:\n",
    "                if r - l >= 3:\n",
    "                    out.append([l,r-1])\n",
    "                l = r\n",
    "                r+= 1\n",
    "        if r - l >= 3: out.append([l,r-1])\n",
    "        return out"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largeGroupPositions(self, s: str) -> List[List[int]]:\n",
    "        l=[]\n",
    "        n=len(s)\n",
    "        a,b=0,0\n",
    "        while(b<n-2):\n",
    "            a=b\n",
    "            if(s[a]==s[a+1]==s[a+2]):\n",
    "                b=a+3\n",
    "                while(b<n and s[b]==s[a]):\n",
    "                    b=b+1\n",
    "                l.append([a,b-1])\n",
    "            else:\n",
    "                b=b+1\n",
    "        return l\n",
    "                    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largeGroupPositions(self, s: str) -> List[List[int]]:\n",
    "        i, j, l = 0, 0, len(s)\n",
    "        res = []\n",
    "        while i < l:\n",
    "            j = i + 1\n",
    "            while j < l and s[j] == s[i]:\n",
    "                j += 1\n",
    "            if j - i > 2:\n",
    "                res.append([i, j - 1])\n",
    "            i = j\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largeGroupPositions(self, s: str) -> List[List[int]]:\n",
    "        m = []\n",
    "        i = 0\n",
    "        while i < len(s)-1:\n",
    "            for j in range(i+1,len(s)):\n",
    "                if s[i] != s[j]:\n",
    "                    if j - i -1 >= 2:\n",
    "                        m.append([i,j-1])\n",
    "                    i = j\n",
    "                    break\n",
    "                if j == len(s)-1:\n",
    "                    if j - i >= 2:\n",
    "                        m.append([i, j])\n",
    "                    i = j\n",
    "                    break\n",
    "        return m"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largeGroupPositions(self, s: str) -> List[List[int]]:\n",
    "        n, num = len(s), 1\n",
    "        res = []\n",
    "        for i in range(n):\n",
    "            if i == n - 1 or s[i] != s[i+1]:\n",
    "                if num >= 3:\n",
    "                    res.append([i-num+1, i])\n",
    "                num = 1\n",
    "            else:\n",
    "                num += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largeGroupPositions(self, s: str) -> List[List[int]]:\n",
    "        start = 0\n",
    "        end = 1\n",
    "        res = []\n",
    "        count=0\n",
    "        while end < len(s):\n",
    "            if s[start]==s[end]:\n",
    "                count+=1\n",
    "            else:\n",
    "                if count>=2:\n",
    "                    res.append([start,end-1])\n",
    "                count=0\n",
    "                start=end\n",
    "            if count>=2 and end==len(s)-1:\n",
    "                res.append([start, end])\n",
    "            end+=1\n",
    "        return res "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largeGroupPositions(self, s: str) -> List[List[int]]:\n",
    "        result = []\n",
    "        left = right = 0\n",
    "        while left < len(s):\n",
    "            while right < len(s) and s[left] == s[right]:\n",
    "                right += 1\n",
    "            if right - left >= 3:\n",
    "                result.append([left, right - 1])\n",
    "            left = right\n",
    "        \n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largeGroupPositions(self, s: str) -> List[List[int]]:\n",
    "        ans = []\n",
    "        n = len(s)\n",
    "        i = 0\n",
    "        while i < n:\n",
    "            start = i\n",
    "            while i < n - 1 and s[i] == s[i + 1]:\n",
    "                i += 1\n",
    "            if i - start >= 2:\n",
    "                ans.append([start, i])\n",
    "            i += 1\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largeGroupPositions(self, s: str) -> List[List[int]]:\n",
    "        if len(s)<=2:\n",
    "            return []\n",
    "        res=[]\n",
    "        l=0\n",
    "        r=0\n",
    "\n",
    "        for i in range(1,len(s)):\n",
    "            if s[i]==s[i-1]:\n",
    "                r+=1\n",
    "            else:\n",
    "                if r-l>=2:\n",
    "                    res.append([l,r])\n",
    "                l=i\n",
    "                r=i\n",
    "        \n",
    "        if r-l>=2:\n",
    "            res.append([l,r])\n",
    "        \n",
    "        return res\n",
    "\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largeGroupPositions(self, s: str) -> List[List[int]]:\n",
    "        i = 0\n",
    "        res = []\n",
    "        while i<len(s):\n",
    "            ans = 1\n",
    "            tmp = -1\n",
    "            t1 = i\n",
    "            for j in range(i+1, len(s)):\n",
    "                if s[i] == s[j]:\n",
    "                    ans += 1\n",
    "                    tmp = j\n",
    "                    i += 1\n",
    "                else:\n",
    "                    i = j - 1\n",
    "                    break\n",
    "            if ans>2:\n",
    "                res.append([t1, tmp])\n",
    "            i += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largeGroupPositions(self, s: str) -> List[List[int]]:\n",
    "        left,right = 0,0\n",
    "        n = len(s)\n",
    "        i = 1\n",
    "        res = []\n",
    "        while i < n:\n",
    "            while i < n and s[i] == s[i - 1]:\n",
    "                right += 1\n",
    "                i += 1\n",
    "            if right - left + 1 >= 3:\n",
    "                res.append([left, right])\n",
    "            left = i\n",
    "            right = i\n",
    "            i += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largeGroupPositions(self, s: str) -> List[List[int]]:\n",
    "        ret = list()\n",
    "        n, num = len(s), 1\n",
    "\n",
    "        for i in range(n):\n",
    "            if i == n - 1 or s[i] != s[i + 1]:\n",
    "                if num >= 3:\n",
    "                    ret.append([i - num + 1, i])\n",
    "                num = 1\n",
    "            else:\n",
    "                num += 1\n",
    "        \n",
    "        return ret\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largeGroupPositions(self, s: str) -> List[List[int]]:\n",
    "        # res = []\n",
    "        # i = j = 0\n",
    "        # n = len(s)\n",
    "        # while j < n:\n",
    "        #     if s[j] == s[i]:\n",
    "        #         j += 1\n",
    "        #     else:\n",
    "        #         if j - i >= 3:\n",
    "        #             res.append([i, j-1])\n",
    "        #         i = j\n",
    "        # if j - i >= 3:\n",
    "        #     res.append([i, j-1])\n",
    "        # return res\n",
    "\n",
    "        # --------------------------\n",
    "        res = []\n",
    "        i = j = 0\n",
    "        s += ' '\n",
    "        n = len(s)\n",
    "        while j < n:\n",
    "            if s[j] == s[i]:\n",
    "                j += 1\n",
    "            else:\n",
    "                if j - i >= 3:\n",
    "                    res.append([i, j-1])\n",
    "                i = j\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largeGroupPositions(self, s: str) -> List[List[int]]:\n",
    "        # 2层for循环，外层遍历字符串索引，里层遍历相同的相邻字符\n",
    "        res = []\n",
    "        i = 0\n",
    "        while i < len(s):\n",
    "            j = i + 1\n",
    "            while j < len(s) and s[j] == s[i]:\n",
    "                j += 1\n",
    "            if j - i >= 3:\n",
    "                res.append([i, j-1])\n",
    "            i = j # i每次遍历跳到不重复的下一个数的索引\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largeGroupPositions(self, s: str) -> List[List[int]]:\n",
    "        n = len(s)\n",
    "        arr = []\n",
    "        if n < 3:\n",
    "            return arr\n",
    "        s = s + '0'\n",
    "        l, r = 0, 1\n",
    "        while r < len(s):\n",
    "            if s[l] == s[r]:\n",
    "                r += 1\n",
    "            else:\n",
    "                if r - l >= 3:\n",
    "                    arr.append([l, r-1])\n",
    "                l = r\n",
    "                r += 1\n",
    "        return arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largeGroupPositions(self, s: str) -> List[List[int]]:\n",
    "        i, n = 0, len(s)\n",
    "        ans = []\n",
    "        while i < n:\n",
    "            j = i\n",
    "            while j < n and s[j] == s[i]:\n",
    "                j += 1\n",
    "            if j - i >= 3:\n",
    "                ans.append([i, j - 1])\n",
    "            i = j\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largeGroupPositions(self, s: str) -> List[List[int]]:\n",
    "        res = []\n",
    "        i = j = 0\n",
    "        n = len(s)\n",
    "        while j < n:\n",
    "            if s[j] == s[i]:\n",
    "                j += 1\n",
    "            else:\n",
    "                if j - i >= 3:\n",
    "                    res.append([i, j-1])\n",
    "                i = j\n",
    "        if j - i >= 3:\n",
    "            res.append([i, j-1])\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largeGroupPositions(self, s: str) -> List[List[int]]:\n",
    "        res = []\n",
    "        n = len(s)\n",
    "        j = 0\n",
    "        for i in range(1,n + 1):\n",
    "            if i == n or s[i - 1] != s[i]:\n",
    "                width = i - j\n",
    "                if width > 2:\n",
    "                    res.append([j,i-1])\n",
    "                j = i\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largeGroupPositions(self, s: str) -> List[List[int]]:\n",
    "        ret = list()\n",
    "        n, num = len(s), 1\n",
    "\n",
    "        for i in range(n):\n",
    "            if i == n - 1 or s[i] != s[i + 1]:\n",
    "                if num >= 3:\n",
    "                    ret.append([i - num + 1, i])\n",
    "                num = 1\n",
    "            else:\n",
    "                num += 1\n",
    "        \n",
    "        return ret\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largeGroupPositions(self, s: str) -> List[List[int]]:\n",
    "        l, r = 0, 1\n",
    "        out = []\n",
    "        while r < len(s):\n",
    "            if s[r] != s[l]:\n",
    "                if r-l >= 3: out.append([l,r-1])\n",
    "                l  = r\n",
    "            r += 1\n",
    "\n",
    "        if r - l >= 3: out.append([l,r-1])\n",
    "        return out"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largeGroupPositions(self, s: str) -> List[List[int]]:\n",
    "        start = 0\n",
    "        end = start + 1\n",
    "        local = []\n",
    "        res = []\n",
    "        length = 1\n",
    "        def add_group():\n",
    "            if length >=3:\n",
    "                local.append(start)\n",
    "                local.append(end - 1)\n",
    "                res.append(local[:])  \n",
    "            local.clear()\n",
    "        while start<len(s) and end <len(s):\n",
    "            if s[start]==s[end]:\n",
    "                length +=1\n",
    "                end += 1\n",
    "            else:\n",
    "                add_group()\n",
    "                start = end\n",
    "                end = start+1\n",
    "                length = 1\n",
    "                \n",
    "        add_group()\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largeGroupPositions(self, s: str) -> List[List[int]]:\n",
    "        if len(s) < 3:\n",
    "            return []\n",
    "        res = []\n",
    "        n = len(s)\n",
    "        p1, p2 = 0, 0\n",
    "        for i in range(1, n):\n",
    "            if s[i] != s[i - 1]:\n",
    "                p2 = i - 1\n",
    "                if p2 - p1 >= 2:\n",
    "                    res.append([p1, p2])\n",
    "                p1 = i\n",
    "        # 检查最后一个分组\n",
    "        if p1 < n - 1 and n - 1 - p1 >= 2:\n",
    "            res.append([p1, n - 1])\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution(object):\n",
    "    def largeGroupPositions(self, s):\n",
    "        \"\"\"\n",
    "        :type s: str\n",
    "        :rtype: List[List[int]]\n",
    "        \"\"\"\n",
    "        result = []\n",
    "        numLen = 1\n",
    "        s += \"0\"\n",
    "        for i in range(len(s)-1):\n",
    "            if(s[i]==s[i+1]):\n",
    "                numLen += 1\n",
    "            else:\n",
    "                if(numLen>=3):\n",
    "                    result.append([i-numLen+1,i])\n",
    "                numLen = 1\n",
    "            if(len(s)==numLen and numLen>=3):\n",
    "                return [[0,len(s)-1]]\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largeGroupPositions(self, S: str) -> List[List[int]]:\n",
    "        i,result=0,[]\n",
    "        while i<len(S):\n",
    "            j=i+1\n",
    "            while j<len(S) and S[j]==S[i]:\n",
    "                j+=1\n",
    "            if j-i>2:\n",
    "                result.append([i,j-1])\n",
    "            i=j\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largeGroupPositions(self, s: str) -> List[List[int]]:\n",
    "        n=len(s)\n",
    "        ans=[]\n",
    "        start = 0  # idx\n",
    "        for i in range(n):\n",
    "            if s[i] != s[start]:\n",
    "                start = i\n",
    "            if i == n - 1 or s[i] != s[i + 1]:\n",
    "                if i - start + 1 >= 3:\n",
    "                    ans.append([start, i])\n",
    "        return ans\n",
    "\n",
    "            \n",
    "            \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largeGroupPositions(self, s: str) -> List[List[int]]:\n",
    "        #双指针的简答应用\n",
    "        l = 0\n",
    "        res = []\n",
    "        n = len(s)\n",
    "        for r in range(1,n):\n",
    "            if s[r] != s[r-1]:\n",
    "                if r-l >= 3:\n",
    "                    res.append([l,r-1])\n",
    "                l = r\n",
    "            if r == n-1:\n",
    "                if r-l+1 >= 3:\n",
    "                    res.append([l,r])\n",
    "        return res\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largeGroupPositions(self, s: str) -> List[List[int]]:\n",
    "        record = []\n",
    "        num = 1\n",
    "        if len(s) < 3: return record\n",
    "\n",
    "        for i in range(len(s)):\n",
    "            if i == len(s)-1 or s[i] != s[i+1]:\n",
    "                if num >= 3:\n",
    "                    record.append([i-num+1,i])\n",
    "                num = 1\n",
    "            else: num += 1\n",
    "\n",
    "        return record"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largeGroupPositions(self, s: str) -> List[List[int]]:\n",
    "        ret = list()\n",
    "        n,num = len(s),1\n",
    "        for i in range(n):\n",
    "            if i == n-1 or s[i] != s[i+1]:\n",
    "                if num >=3:\n",
    "                    ret.append([i-num+1,i])\n",
    "                num = 1\n",
    "            else:\n",
    "                num +=1\n",
    "        return ret\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largeGroupPositions(self, s: str) -> List[List[int]]:\n",
    "        n = len(s)\n",
    "        ans = []\n",
    "        num = 1\n",
    "        for i in range(n):\n",
    "            if i == n - 1 or s[i] != s[i+1]:\n",
    "                if num >= 3:\n",
    "                    ans.append([i- num + 1, i])\n",
    "                num = 1\n",
    "            else:\n",
    "                num += 1\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largeGroupPositions(self, s: str) -> List[List[int]]:\n",
    "        if len(s)<3:\n",
    "            return []\n",
    "        # if len(set(list(s)))==1:\n",
    "        #     return [[0,len(s)-1]]\n",
    "        s+=' '\n",
    "        res=[]\n",
    "        st=0\n",
    "        v=s[0]\n",
    "        for i in range(1,len(s)):\n",
    "            if s[i]!=v:\n",
    "                if i-1-st>=2:\n",
    "                    res.append([st,i-1])\n",
    "                st=i\n",
    "                v=s[i]\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largeGroupPositions(self, s: str) -> List[List[int]]:\n",
    "        if len(s) < 3:\n",
    "            return []\n",
    "        p1,p2 = 0,0\n",
    "        s += ' '\n",
    "        res = []\n",
    "        for i in range(len(s)-1):\n",
    "            if s[i+1] != s[i]:\n",
    "                p2 = i\n",
    "                if p2 - p1 >= 2:\n",
    "                    res.append([p1,p2])\n",
    "                p1 = i + 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from typing import List\r\n",
    "class Solution:\r\n",
    "    def largeGroupPositions(self, s: str) -> List[List[int]]:\r\n",
    "        start, end = 0, 1\r\n",
    "        n = len(s)\r\n",
    "        res = []\r\n",
    "        while end < n:\r\n",
    "            while end < n and s[start] == s[end]:\r\n",
    "                end += 1\r\n",
    "            if end - start >= 3:\r\n",
    "                res.append([start, end - 1])\r\n",
    "            start = end\r\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largeGroupPositions(self, s: str) -> List[List[int]]:\n",
    "        res = []\n",
    "        n = len(s)\n",
    "        i= 0\n",
    "        while i < n:\n",
    "            j = i + 2\n",
    "            if s[i] * 3 != s[i:j+1]:\n",
    "                i += 1\n",
    "                continue\n",
    "            j += 1\n",
    "            while j < n:\n",
    "                if s[j] != s[j-1]:\n",
    "                    break\n",
    "                j += 1\n",
    "            res.append([i,j-1])\n",
    "            i = j\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largeGroupPositions(self, s: str) -> List[List[int]]:\n",
    "        n = len(s)\n",
    "        cur_num = 1\n",
    "        ans = []\n",
    "        for i in range(n):\n",
    "            if i == n-1 or s[i] != s[i+1]:\n",
    "                if cur_num >= 3:\n",
    "                    ans.append([i-cur_num+1, i])\n",
    "                cur_num = 1\n",
    "            else:\n",
    "                cur_num += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largeGroupPositions(self, s: str) -> List[List[int]]:\n",
    "        ans = []\n",
    "        n = len(s)\n",
    "        i = 0\n",
    "        while i < n:\n",
    "            start = i\n",
    "            while i < n - 1 and s[i] == s[i + 1]:\n",
    "                i += 1\n",
    "            if i - start > 1:\n",
    "                ans.append([start, i])\n",
    "            i += 1\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largeGroupPositions(self, s: str) -> List[List[int]]:\n",
    "        if len(s)<3:\n",
    "            return []\n",
    "        if len(set(list(s)))==1:\n",
    "            return [[0,len(s)-1]]\n",
    "        s+=' '\n",
    "        res=[]\n",
    "        st=0\n",
    "        v=s[0]\n",
    "        for i in range(1,len(s)):\n",
    "            if s[i]!=v:\n",
    "                if i-1-st>=2:\n",
    "                    res.append([st,i-1])\n",
    "                st=i\n",
    "                v=s[i]\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largeGroupPositions(self, s: str) -> List[List[int]]:\n",
    "        if len(s) < 3:\n",
    "            return []\n",
    "        res = []\n",
    "        n = len(s)\n",
    "        p1, p2 = 0, 0\n",
    "        s = s + ' '\n",
    "        for i in range(n):\n",
    "            if s[i+1]!= s[i]:\n",
    "                p2 = i\n",
    "                if p2-p1 >= 2:\n",
    "                    res.append([p1, p2])\n",
    "                p1 = i+1\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largeGroupPositions(self, s: str) -> List[List[int]]:\n",
    "        res = []\n",
    "        i = 0\n",
    "        while i < len(s):\n",
    "            cur = s[i]\n",
    "            temp = i\n",
    "            while temp < len(s) and s[temp] == cur:\n",
    "                temp += 1\n",
    "            if temp - i > 2:\n",
    "                res.append([i, temp - 1])\n",
    "            i = temp\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largeGroupPositions(self, s: str) -> List[List[int]]:\n",
    "        n = len(s)\n",
    "        num = 1\n",
    "        res = []\n",
    "        for i in range(n):\n",
    "            if i == n-1 or s[i] != s[i+1]:\n",
    "                if num >= 3:\n",
    "                    res.append([i-num+1,i])\n",
    "                num = 1\n",
    "            else:\n",
    "                num += 1\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largeGroupPositions(self, s: str) -> List[List[int]]:\n",
    "\n",
    "        d = collections.defaultdict(list)\n",
    "        res = []\n",
    "\n",
    "        for i in range(len(s)):\n",
    "            if s[i] not in d:\n",
    "                d[s[i]] = [i, 0]\n",
    "\n",
    "                if i > 0:\n",
    "                    tmp = d[s[i - 1]]\n",
    "                    if d[s[i - 1]][-1] >= 2:\n",
    "                        res.append([tmp[0], tmp[0] + tmp[1]])\n",
    "                    del d[s[i - 1]]\n",
    "            else:\n",
    "                d[s[i]][-1] += 1\n",
    "\n",
    "        if d[s[-1]][1]> 1:\n",
    "            res.append([d[s[-1]][0], d[s[-1]][0] + d[s[-1]][1]])\n",
    "\n",
    "        return res\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largeGroupPositions(self, s: str) -> List[List[int]]:\n",
    "        res=[]\n",
    "        i=0\n",
    "        while i<len(s):\n",
    "            j=i+1\n",
    "            while j<len(s) and s[j]==s[i]:\n",
    "                j+=1\n",
    "            if j-i>=3:\n",
    "                res.append([i,j-1])\n",
    "            i=j\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largeGroupPositions(self, s: str) -> List[List[int]]:\n",
    "        l = len(s);\n",
    "        res = [];\n",
    "        if l < 3:\n",
    "            return res;\n",
    "\n",
    "        ptr1 = ptr2 = 0;\n",
    "        while ptr2 < l:\n",
    "            if s[ptr2] != s[ptr1]:\n",
    "                if ptr2 - ptr1 > 2:\n",
    "                    res.append([ptr1, ptr2 - 1]);\n",
    "                ptr1 = ptr2;\n",
    "            elif ptr2 == l - 1 and ptr2 - ptr1 > 1:\n",
    "                res.append([ptr1, ptr2]);\n",
    "            \n",
    "            ptr2 += 1;\n",
    "\n",
    "        return res;    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largeGroupPositions(self, s: str) -> List[List[int]]:\n",
    "\n",
    "        d = collections.defaultdict(list)\n",
    "        res = []\n",
    "\n",
    "        for i in range(len(s)):\n",
    "            if s[i] not in d:\n",
    "                d[s[i]] = [i, 0]\n",
    "\n",
    "                if i > 0:\n",
    "                    tmp = d[s[i - 1]]\n",
    "                    if d[s[i - 1]][-1] >= 2:\n",
    "                        res.append([tmp[0], tmp[0] + tmp[1]])\n",
    "                    del d[s[i - 1]]\n",
    "            else:\n",
    "                d[s[i]][-1] += 1\n",
    "\n",
    "        if d[s[-1]][1]> 1:\n",
    "            res.append([d[s[-1]][0], d[s[-1]][0] + d[s[-1]][1]])\n",
    "\n",
    "        return res\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largeGroupPositions(self, s: str) -> List[List[int]]:\n",
    "        n = len(s)\n",
    "        res = []\n",
    "        num = 1\n",
    "        for i in range(n):\n",
    "            if i == n-1 or s[i] != s[i+1]:\n",
    "                if num >= 3:\n",
    "                    res.append([i-num+1, i])\n",
    "                num = 1\n",
    "            else:\n",
    "                num += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largeGroupPositions(self, s: str) -> List[List[int]]:\n",
    "        res=[]\n",
    "        if len(s) < 3:\n",
    "            return res\n",
    "        num=1\n",
    "        for i in range(len(s)):\n",
    "            if i == len(s) - 1 or s[i] != s[i + 1]:\n",
    "                if num>=3:\n",
    "                  res.append([i-num+1,i])\n",
    "                num=1\n",
    "            else:\n",
    "                num+=1\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largeGroupPositions(self, s: str) -> List[List[int]]:\n",
    "        res = []\n",
    "\n",
    "        if len(s) < 3:\n",
    "            return res\n",
    "        num = 1\n",
    "\n",
    "        for i in range(len(s)):\n",
    "            if i == len(s) - 1 or s[i] != s[i + 1]:\n",
    "                if num >= 3:\n",
    "                    res.append([i - num + 1, i])\n",
    "                num = 1\n",
    "            else:\n",
    "                num += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largeGroupPositions(self, s: str) -> List[List[int]]:\n",
    "        if len(s) < 3:\n",
    "            return []\n",
    "        \n",
    "        res = []\n",
    "        n = len(s)\n",
    "        p1, p2 = 0, 0\n",
    "        s = s + ' '\n",
    "\n",
    "        for i in range(n):\n",
    "            if s[i + 1] != s[i]:\n",
    "                p2 = i\n",
    "                if p2 - p1 >= 2:\n",
    "                    res.append([p1, p2])\n",
    "                p1 = i + 1\n",
    "\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largeGroupPositions(self, s: str) -> List[List[int]]:\n",
    "        # 对一个元素，固定第一个指针，从该指针起遍历，直到第二个指针不等于它\n",
    "        l = 0\n",
    "        r = 1\n",
    "        out = []\n",
    "        while r < len(s):\n",
    "            if s[l] == s[r]:\n",
    "                r += 1\n",
    "            else:\n",
    "                if r - l >= 3:\n",
    "                    out.append([l,r-1])\n",
    "                l = r\n",
    "                r+= 1\n",
    "        if r - l >= 3: out.append([l,r-1])\n",
    "        return out"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largeGroupPositions(self, s: str) -> List[List[int]]:\n",
    "        out = []\n",
    "        end = count = 0\n",
    "        tmp = s[0]\n",
    "        for i in range(len(s)):\n",
    "            if s[i] == tmp:\n",
    "                end = i\n",
    "                count += 1\n",
    "            else:\n",
    "                tmp = s[i]\n",
    "                if count >= 3:\n",
    "                    out.append([end-count+1, end])\n",
    "                count = 1\n",
    "        if count >= 3:  # 考虑 如果字符串的最后几个是连续字符，则无法进入上面 else 的情况(eg.s5, s6)\n",
    "            out.append([end - count + 1, end])\n",
    "        return out"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largeGroupPositions(self, s: str) -> List[List[int]]:\n",
    "        n = len(s)\n",
    "        num = 1\n",
    "        res = []\n",
    "        if len(s) < 3:\n",
    "            return res\n",
    "        for i in range(n):\n",
    "            if i == n-1 or s[i] != s[i+1]:\n",
    "                if num >= 3:\n",
    "                    res.append([i-num+1,i])\n",
    "                num = 1\n",
    "            else:\n",
    "                num += 1\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largeGroupPositions(self, s: str) -> List[List[int]]:\n",
    "        n = len(s)\n",
    "        ans = []\n",
    "        i = 0\n",
    "        while i < n:\n",
    "            start = i\n",
    "            i += 1\n",
    "            while i < n and s[i] == s[i - 1]:\n",
    "                i += 1\n",
    "            if i - start >= 3:\n",
    "                ans.append([start, i - 1])\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largeGroupPositions(self, s: str) -> List[List[int]]:\n",
    "        res = []\n",
    "        i = 0\n",
    "        while i in range(len(s)):\n",
    "            count = 1\n",
    "            for j in range(i+1,len(s)):\n",
    "                \n",
    "                if s[i] == s[j]:\n",
    "                    count = count + 1\n",
    "                else:\n",
    "                    if count>=3:\n",
    "                        res.append([i,j-1])\n",
    "                    break\n",
    "                if j == len(s)-1 and count>=3:\n",
    "                    res.append([i,len(s)-1])\n",
    "            if count == 0:\n",
    "                i = i+1\n",
    "            else:\n",
    "                i = i+count\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largeGroupPositions(self, s: str) -> List[List[int]]:\n",
    "\n",
    "        d = collections.defaultdict(list)\n",
    "        res = []\n",
    "\n",
    "\n",
    "\n",
    "        for i in range(len(s)):\n",
    "            if s[i] not in d:\n",
    "                d[s[i]] = [i, 0]\n",
    "\n",
    "                if i > 0:\n",
    "                    if d[s[i - 1]][-1] >= 2:\n",
    "                        res.append([d[s[i - 1]][0], d[s[i - 1]][0] + d[s[i - 1]][1]])\n",
    "                    del d[s[i - 1]]\n",
    "            else:\n",
    "                d[s[i]][-1] += 1\n",
    "\n",
    "        if d[s[-1]][1]> 1:\n",
    "            res.append([d[s[-1]][0], d[s[-1]][0] + d[s[-1]][1]])\n",
    "\n",
    "        return res\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largeGroupPositions(self, s: str) :\n",
    "        i = 0\n",
    "        cnt = 0\n",
    "        nums = []\n",
    "        for j in range(len(s)):\n",
    "            if s[j] == s[i]:\n",
    "                cnt += 1\n",
    "            else:\n",
    "                nums.append(cnt)\n",
    "                cnt = 1\n",
    "                i = j\n",
    "        nums.append(len(s)-i)\n",
    "        return [[sum(nums[:i]),sum(nums[:i+1])-1]for i in range(len(nums)) if nums[i] >= 3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largeGroupPositions(self, s: str) -> List[List[int]]:\n",
    "        n = len(s)\n",
    "        num = 1\n",
    "        res = []\n",
    "        for i in range(n):\n",
    "            if i == n-1 or s[i] != s[i+1]:\n",
    "                if num >= 3:\n",
    "                    res.append([i-num+1,i])\n",
    "                num = 1\n",
    "            else:\n",
    "                num += 1\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largeGroupPositions(self, s: str) -> List[List[int]]:\n",
    "        n = len(s)\n",
    "        num = 1\n",
    "        res = []\n",
    "        for i in range(n):\n",
    "            if i == n-1 or s[i] != s[i+1]:\n",
    "                if num >= 3:\n",
    "                    res.append([i-num+1,i])\n",
    "                num = 1\n",
    "            else:\n",
    "                num += 1\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "\tdef largeGroupPositions(self, s):\n",
    "\t\tres, j = [], 0\n",
    "\t\tfor i in range(1, len(s) + 1):\n",
    "\t\t\tif i == len(s) or s[i] != s[j]:\n",
    "\t\t\t\tif i - j >= 3: res.append([j, i - 1])\n",
    "\t\t\t\tj = i\n",
    "\t\treturn res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largeGroupPositions(self, s: str) -> List[List[int]]:\n",
    "        n = len(s)\n",
    "        i, j = 0, 0\n",
    "        res = []\n",
    "        while j < n:\n",
    "            count = 0\n",
    "            while j < n and s[i] == s[j]:\n",
    "                count += 1\n",
    "                j += 1\n",
    "            if count >= 3:\n",
    "                res.append([i, j-1])\n",
    "            i = j\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largeGroupPositions(self, s: str) -> List[List[int]]:\n",
    "        i_start = 0\n",
    "        i_end = 0\n",
    "        res = []\n",
    "        i = 1\n",
    "        while i <= len(s):\n",
    "            if i == len(s) or s[i] != s[i-1]:\n",
    "                i_end = i-1\n",
    "                if i_end - i_start >= 2:\n",
    "                    res.append([i_start, i_end])\n",
    "                i_start = i\n",
    "            i += 1\n",
    "        return res\n",
    "                    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largeGroupPositions(self, s: str) -> List[List[int]]:\n",
    "        if len(s)<3:\n",
    "            result=[]\n",
    "\n",
    "        result=[]\n",
    "        n=len(s)\n",
    "        p1,p2=0,0\n",
    "        s=s+\" \"\n",
    "        for i in range(n):\n",
    "            if s[i+1]!=s[i]:\n",
    "                p2=i\n",
    "                if p2-p1>=2:\n",
    "                    result.append([p1,p2])\n",
    "                p1=i+1\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largeGroupPositions(self, s: str) -> List[List[int]]:\n",
    "        counter=1\n",
    "        location={s[0]:0}\n",
    "        res=[]\n",
    "        if len(s)<3:\n",
    "            return []\n",
    "        for i in range(1,len(s)):\n",
    "            if s[i]==s[i-1]:\n",
    "                counter+=1\n",
    "                if counter==len(s) and counter>2:\n",
    "                    return [[0,counter-1]]\n",
    "                if counter>2 and i<len(s)-1 and s[i+1]!=s[i]:\n",
    "                   res.append([location[s[i]],i])\n",
    "            else:\n",
    "                location[s[i]]=i\n",
    "                counter=1\n",
    "        if counter>=2:#判断最后一个字符\n",
    "            if s[-1]==s[-3]:\n",
    "                res.append([location[s[-1]],len(s)-1])\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largeGroupPositions(self, s: str) -> List[List[int]]:\n",
    "        ans = list()\n",
    "        length = 0\n",
    "        last = s[0]\n",
    "        for index, x in enumerate(s):\n",
    "            if x == last:\n",
    "                length += 1\n",
    "            else:\n",
    "                if length > 2:\n",
    "                    ans.append([index - length, index - 1])\n",
    "                \n",
    "                print(last, length)\n",
    "                length = 1\n",
    "                last = x\n",
    "        if length == len(s) and len(s) > 2:\n",
    "            return [[0, len(s) - 1]]\n",
    "        if length > 2:\n",
    "            ans.append([len(s) - length, len(s) - 1])\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largeGroupPositions(self, s: str) -> List[List[int]]:\n",
    "        tmp, ans = 0, []\n",
    "        for i in range(len(s)):\n",
    "            if s[i] != s[tmp]:\n",
    "                if i-tmp < 3:\n",
    "                    tmp = i\n",
    "                    continue\n",
    "                else:\n",
    "                    ans.append([tmp,i-1])\n",
    "                    tmp = i\n",
    "        if len(s)-tmp >= 3:\n",
    "            ans.append([tmp,len(s)-1])\n",
    "        return ans"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
